package com.qingfeng.qfcms.page;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页数据
 * @param <T>
 */
public class Page<T> {
    private int pageNo = 1;			//页码，默认是第一页
    private int pageSize = 10;		//每页显示的记录数，默认是10
    private int totalRecord;		//总记录数
    private int totalPage;			//总页数
    private List<T> results;		//对应的当前页记录
    private Map<String, Object> params = new HashMap<String, Object>();		//其他的参数我们把它分装成一个Map对象
 
    public int getPageNo() {
       return pageNo;
    }
 
    public void setPageNo(int pageNo) {
       this.pageNo = pageNo;
    }
 
    public int getPageSize() {
       return pageSize;
    }
 
    public void setPageSize(int pageSize) {
       this.pageSize = pageSize;
    }
 
    public int getTotalRecord() {
       return totalRecord;
    }
 
    public void setTotalRecord(int totalRecord) {
       this.totalRecord = totalRecord;
       //在设置总页数的时候计算出对应的总页数，在下面的三目运算中加法拥有更高的优先级，所以最后可以不加括号。
       int totalPage = totalRecord%pageSize==0 ? totalRecord/pageSize : totalRecord/pageSize + 1;
       this.setTotalPage(totalPage);
    }
 
    public int getTotalPage() {
       return totalPage;
    }
 
    public void setTotalPage(int totalPage) {
       this.totalPage = totalPage;
    }
 
    public List<T> getResults() {
       return results;
    }
 
    public void setResults(List<T> results) {
       this.results = results;
    }
   
    public Map<String, Object> getParams() {
       return params;
    }
   
    public void setParams(Map<String, Object> params) {
       this.params = params;
    }
 
    public String toString() {
       StringBuilder builder = new StringBuilder();
       builder.append("Page [pageNo=").append(pageNo).append(", pageSize=").append(pageSize).append(", results=").append(results).append(", totalPage=").append(totalPage).append(", totalRecord=").append(totalRecord).append("]");
       return builder.toString();
    }
 
	/* 页面链接 */
    public String getPageStr(String url) {
        StringBuffer sb = new StringBuffer();
        if(totalRecord>0){
            sb.append("	<ul>\n");
            if(pageNo==1){
                sb.append("	<li><a>共<font color=red>"+totalRecord+"</font>条</a></li>\n");
                sb.append("	<li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:50px;text-align:center;float:left\" placeholder=\"页码\"/></li>\n");
                sb.append("	<li style=\"cursor:pointer;\"><a onclick=\"toTZ();\"  class=\"btn btn-mini btn-success\">跳转</a></li>\n");

                sb.append("	<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo=1");
                sb.append("\">首页");
                sb.append("</a>");
                sb.append("</li>");

                sb.append("	<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo=");
                if(pageNo<=1){
                    sb.append(1);
                }else{
                    sb.append(pageNo-1);
                }
                sb.append("\">上页");
                sb.append("</a>&nbsp;");
                sb.append("</li>");



                //sb.append("	<li style=\"cursor:pointer;\"><a onclick=\"nextPage("+(currentPage-1)+")\">上页</a></li>\n");
            }else{
                sb.append("	<li><a>共<font color=red>"+pageNo+"</font>条</a></li>\n");
                sb.append("	<li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:50px;text-align:center;float:left\" placeholder=\"页码\"/></li>\n");
                sb.append("	<li style=\"cursor:pointer;\"><a onclick=\"toTZ();\"  class=\"btn btn-mini btn-success\">跳转</a></li>\n");
                sb.append("	<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo=1");
                sb.append("\">首页");
                sb.append("</a>");
                sb.append("</li>");

                sb.append("	<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo=");
                if(pageNo<=1){
                    sb.append(1);
                }else{
                    sb.append(pageNo-1);
                }
                sb.append("\">上页");
                sb.append("</a>&nbsp;");
                sb.append("</li>");
            }
            int showTag = 5;//分页标签显示数量
            int startTag = 1;
            if(pageNo>showTag){
                startTag = pageNo-1;
            }
            int endTag = startTag+showTag-1;
            for(int i=startTag; i<=totalPage && i<=endTag; i++){
                if(pageNo==i){
                    sb.append("<li>");
                    sb.append("<a href=\"").append(url).append("?pageNo="+i);
                    sb.append("\"<font color='#808080'>"+i+"</font>");
                    sb.append("</a>");
                    sb.append("</li>\n");
                }
                else{
                    sb.append("<li>");
                    sb.append("<a href=\"").append(url).append("?pageNo="+i);
                    sb.append("\"<font color='#808080'>"+i+"</font>");
                    sb.append("</a>");
                    sb.append("</li>\n");
                }

            }
            if(pageNo==totalPage){
                sb.append("<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo="+(pageNo+1));
                sb.append("\">下页");
                sb.append("</a>");
                sb.append("</li>\n");

                sb.append("<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo="+totalPage);
                sb.append("\">尾页");
                sb.append("</a>");
                sb.append("</li>\n");
            }else{
                sb.append("<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo="+(pageNo+1));
                sb.append("\">下页");
                sb.append("</a>");
                sb.append("</li>\n");

                sb.append("<li style=\"cursor:pointer;\">");
                sb.append("<a href=\"").append(url).append("?pageNo="+totalPage);
                sb.append("\">尾页");
                sb.append("</a>");
                sb.append("</li>\n");
            }
            sb.append("	<li><a>第"+pageNo+"页</a></li>\n");
            sb.append("	<li><a>共"+totalPage+"页</a></li>\n");

        }
        return sb.toString();
    }

}